package br.com.guardaroupavirtual.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;

public class BaseDAO<T> implements IBaseDAO<T> {

	protected Session session;
	protected Class<T> classe;
	
	public BaseDAO(Session session, Class<T> classe) {
		this.session = session;
		this.classe = classe;
	}
	
	@Override
	public void cadastrar(T bean) {
		this.session.save(bean);		
	}

	@Override
	public void alterar(T bean) {
		this.session.update(bean);
	}

	@Override
	public void deletar(T bean) {
		this.session.delete(bean);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T load(Serializable id) {
		 return (T) this.session.load(classe, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> listar() {
		Criteria c = this.session.createCriteria(classe);
		return c.list();
	}

}
